package sg.com.techstudio.cfoconnect.excel;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import sg.com.techstudio.cfoconnect.controller.BaseController;
import sg.com.techstudio.cfoconnect.obj.RegisterObj;

@Component("reportRegistrationExcel")
public class ReportRegistrationExcel extends BaseExcel {

	private static final Log log = LogFactory.getLog(ReportRegistrationExcel.class);
	
	@Override
	protected String getFilename() {
		return "registration_"+FILENAME_SUFFIX_SDF.format(Calendar.getInstance().getTime())+EXCEL_EXTENSION;
	}

	@Override
	protected String getTabname() {
		return "registration";
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			WritableWorkbook wb, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		log.debug("buildExcelDocument() entry");
		
		processServletRequestResponse(req, resp);
        WritableSheet ws = prepareWritableSheet(wb);

        int row = 0, col=0;
        List<RegisterObj> regList = (List<RegisterObj>) model.get(BaseController.PARAM_CONTENT_LIST);

        writeColumnHeader(ws, row++, col, new String[]{"Reg Id", "Register Date","Updated Date"
        		,"Salutation", "Full Name", "First Name", "Last Name", "Job Title", "Company"
        		, "Email", "Tel", "Mobile", "PA Name", "PA Email", "PA Tel", "Dietary"
        		, "Amount", "Payment Status", "Paypal Payer", "Status"});
        if(regList!=null)
        for(RegisterObj r: regList) {
        	col=0;
        	ws.addCell(new jxl.write.Number(col++, row, r.getId()));
        	if(r.getCreatedDate()==null)
        		col++;
        	else
        		ws.addCell(new DateTime(col++, row, gmtDateTime(r.getCreatedDate())));
        	if(r.getUpdatedDate()==null)
        		col++;
        	else
        		ws.addCell(new DateTime(col++, row, gmtDateTime(r.getUpdatedDate())));
        	ws.addCell(new Label(col++, row, r.getSalutation()));
        	ws.addCell(new Label(col++, row, r.getFullName()));
        	ws.addCell(new Label(col++, row, r.getFirstName()));
        	ws.addCell(new Label(col++, row, r.getLastName()));
        	ws.addCell(new Label(col++, row, r.getDesignation()));
        	ws.addCell(new Label(col++, row, r.getCompanyName()));
        	ws.addCell(new Label(col++, row, r.getEmail()));
        	ws.addCell(new Label(col++, row, r.getTelephone()));
        	ws.addCell(new Label(col++, row, r.getMobile()));
        	ws.addCell(new Label(col++, row, r.getPaName()));
        	ws.addCell(new Label(col++, row, r.getPaEmail()));
        	ws.addCell(new Label(col++, row, r.getPaTelephone()));
        	ws.addCell(new Label(col++, row, r.getDietary()));
        	ws.addCell(new jxl.write.Number(col++, row, r.getAmount()==null?0:r.getAmount(), dollarFormat()));
        	ws.addCell(new Label(col++, row, paymentStatus(r.getStatus())));
        	ws.addCell(new Label(col++, row, r.getPayerEmail()));
        	ws.addCell(new Label(col++, row, status(r.getStatus())));
        	row++;
        }
        
       
        
        log.debug("buildExcelDocument() exit");
	}
	
	protected String paymentStatus(int paymentStatus) {
		switch(paymentStatus) {
		case 1:
			return "Completed";
		}
		return "";
	}
	
	protected String status(int status) {
		switch(status) {
		case 0:
			return "Pending";
		case 1:
			return "Confirmed";
		case 2:
			return "Cancelled";
		}
		return ""+status;
	}
	
}
